extends CharacterBody2D

@export var player_id = 1
@export var max_speed = 300.0
@export var acceleration = 1200.0  # 增加加速度，让响应更快
@export var friction = 800.0       # 增加摩擦力，减少滑冰感
@export var jump_velocity = -400.0
@export var gravity = 900.0

var can_hit = true
var hit_cooldown = 0.5

@onready var animation_player = $AnimationPlayer

func _ready():
	# 设置玩家特定的输入映射
	if player_id == 1:
		# 玩家1使用WASD
		pass
	else:
		# 玩家2使用方向键
		pass

	

func _physics_process(delta):
	var direction = Vector2.ZERO

	# 水平移动输入
	if player_id == 1:
		if Input.is_action_pressed("ui_left"):
			direction.x -= 1
		if Input.is_action_pressed("ui_right"):
			direction.x += 1
		# 跳跃逻辑
		if Input.is_action_just_pressed("ui_up"):
			if is_on_floor():
				velocity.y = jump_velocity
		if Input.is_action_just_pressed("p1_hit_overhead"):
			animation_player.play("hit_overhead")
		if Input.is_action_just_pressed("p1_hit_under"):
			animation_player.play("hit_under")
			# 可在动画关键帧或延迟后调用击球逻辑

	else:
		if Input.is_action_pressed("ui_text_caret_left"):
			direction.x -= 1
		if Input.is_action_pressed("ui_text_caret_right"):
			direction.x += 1
		if Input.is_action_just_pressed("ui_text_caret_up"):
			if is_on_floor():
				velocity.y = jump_velocity
		if Input.is_action_just_pressed("p2_hit_overhead"):
			animation_player.play("hit_overhead")
		if Input.is_action_just_pressed("p2_hit_under"):
			animation_player.play("hit_under")

	# 应用加速度和惯性
	apply_movement_acceleration(direction, delta)

	# 重力
	if not is_on_floor():
		velocity.y += gravity * delta
	else:
		# 防止下落速度累积
		if velocity.y > 0:
			velocity.y = 0

	# 移动
	move_and_slide()

	# 更新击球冷却
	if not can_hit:
		hit_cooldown -= delta
		if hit_cooldown <= 0:
			can_hit = true
			hit_cooldown = 0.5
			
	position = position.clamp(Vector2.ZERO, get_viewport_rect().size)


func apply_movement_acceleration(direction: Vector2, delta: float):
	# 计算目标速度
	var target_velocity = direction * max_speed
	
	# 应用加速度和摩擦力
	if direction.x != 0:
		# 有输入时，向目标速度加速
		velocity.x = move_toward(velocity.x, target_velocity.x, acceleration * delta)
	else:
		# 无输入时，应用更强的摩擦力减速
		velocity.x = move_toward(velocity.x, 0, friction * delta)
		
		# 如果速度很小，直接设为0，避免微小抖动
		if abs(velocity.x) < 10:
			velocity.x = 0

func hit_ball(ball: RigidBody2D):
	if not can_hit:
		return
	
	can_hit = false
	
	# 计算击球方向和力度
	var direction_to_ball = (ball.global_position - global_position).normalized()
	var distance = global_position.distance_to(ball.global_position)
	
	# 根据距离调整力度
	var force = 400.0
	if distance < 50:
		force = 300.0
	elif distance > 100:
		force = 500.0
	
	# 添加一些随机性
	var random_angle = randf_range(-0.2, 0.2)
	direction_to_ball = direction_to_ball.rotated(random_angle)
	
	# 应用击球力
	ball.apply_central_impulse(direction_to_ball * force)
	
	# 播放击球音效（如果有的话）
	# play_hit_sound()

func hit_ball_manual():
	# 手动击球（当按下击球键时）
	var ball = get_parent().get_node("Ball")
	if ball and can_hit:
		var direction_to_ball = (ball.global_position - global_position).normalized()
		var force = 350.0
		ball.apply_central_impulse(direction_to_ball * force)
		can_hit = false

# func play_hit_sound():
# 	if has_node("HitSound"):
# 		$HitSound.play() 
